<?php 

class Color 
{

  var $r;
  var $g;
  var $b;
  var $a;

  function Color($color)
  {
		$this->set($color);
  }

  
  function setRGBA($r, $g, $b, $a)
  {
		$this->r = $r;
		$this->g = $g;
		$this->b = $b;
		$this->a = $a;
  }

  public function set($val)
  {
		list($a, $r, $g, $b) = $this->getRGBFromIntA($val);
    $this->a = $a;
    $this->r = $r;
    $this->g = $g;
    $this->b = $b;
    return this;
  }

  public function darken($amount)
  {
		$this->r = $this->limit($this->r - $amount);
		$this->g = $this->limit($this->g - $amount);
		$this->b = $this->limit($this->b - $amount);
  }

  public function lighten($amount)
  {
		$this->r = $this->limit($this->r + $amount);
		$this->g = $this->limit($this->g + $amount);
		$this->b = $this->limit($this->b + $amount);
  }

  private function limit($val)
  {
		if ($val > 255) return 255;
		else if ($val < 0) return 0;
		return $val;
  }

	public function toInt()
  {
		return ($this->r << 16 | $this->g << 8 | $this->b);
  }

	public function toIntA()
  {
		return ($this->a << 24 | $this->r << 16 | $this->g << 8 | $this->b);
  }
	
	public function getRGBFromInt($val)
	{
    $r = ($val >> 16) & 0xFF;
    $g = ($val >> 8) & 0xFF;
    $b = $val & 0xFF;	
		return array($r, $g, $b);
	}

	
	public function getRGBFromIntA($int)
	{
    $a = ($val >> 24) & 0xFF;
    $r = ($val >> 16) & 0xFF;
    $g = ($val >> 8) & 0xFF;
    $b = $val & 0xFF;	
		return array($a, $r, $g, $b);
	}
}

?>